﻿using System;
using System.Linq;
using System.Xml.Linq;
using System.Net.Http;
using OurDailyBread.Helpers;
using Windows.Data.Json;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text;
using System.Net.Http.Headers;
using Newtonsoft.Json.Converters;

namespace OurDailyBread.Model
{
    public class LikeDataService : ILikeDataService
    {
        //string breadWebAPIUri = "http://localhost:50243/api/likes";
        //string UnlikeAPIUri = "http://localhost:50243/api/unlikes";
        string breadWebAPIUri = "http://breadwebapi.azurewebsites.net/api/likes";
        string UnlikeAPIUri = "http://breadwebapi.azurewebsites.net/api/unlikes";

        public async void GetData(DateTime likeTime, Action<Object, Exception> callback)
        {
            // Use this to connect to the actual data service
            try
            {
                string BreadDate = Util.GetShortNavUrl(likeTime);
                string url = breadWebAPIUri + "?BreadDate=" + BreadDate;
                string getResults = await HttpService.Get(new Uri(url));

                //IEnumerable<CommentData> results = JsonConvert.DeserializeObject<IEnumerable<CommentData>>(getResults);

                callback(getResults, null);
            }
            catch (Exception ex)
            {
                callback(null, ex);
            }

        }

        public async void Like(DateTime likeTime, Action<Object, Exception> callback)
        {
            // Use this to connect to the actual data service
            try
            {
                HttpResponseMessage msg;

                LikeData like = new LikeData();
                DateTime actionTime = DateTime.UtcNow;
                like.PartitionKey = actionTime.ToString("yyyy");
                like.RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - actionTime.Ticks, Guid.NewGuid());
                like.Timestamp = actionTime.ToString();
                like.BreadDate = Util.GetShortNavUrl(likeTime);
                App app = (App)OurDailyBread.App.Current;
                like.BreadTitle = app.Context.BreadTitle;
                like.ShortContent = app.Context.ShortContent;
                like.VerseTitle = app.Context.VerseTitle;
                like.ShortVerse = app.Context.ShortVerse;

                string jsonLike = JsonConvert.SerializeObject(like);
                StringContent content = new StringContent(jsonLike, Encoding.UTF8, "application/json");

                using (var client = new HttpClient())
                { 
                    msg = await client.PostAsync(new Uri(breadWebAPIUri), content);
                }

                callback(msg, null);
            }
            catch (Exception ex)
            {
                callback(null, ex);
            }

        }

        public async void Unlike(DateTime likeTime, Action<Object, Exception> callback)
        {
            try
            {
                HttpResponseMessage msg;
                LikeData like = new LikeData();
                like.BreadDate = Util.GetShortNavUrl(likeTime);
                string jsonLike = JsonConvert.SerializeObject(like);
                StringContent content = new StringContent(jsonLike, Encoding.UTF8, "application/json");

                using (var client = new HttpClient())
                {
                    msg = await client.PostAsync(new Uri(UnlikeAPIUri), content);
                }

                callback(msg, null);
            }
            catch (Exception ex)
            {
                callback(null, ex);
            }
        }

    }
}